﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{

	public class TraikyDAO:IData<Traiky> 
 	{
		public TraikyDAO(String myconnecString) : base(myconnecString) { }

		protected override int InsertOnSubmit(ref Traiky item, int t) {
			 SqlParameter p = new SqlParameter(); 
			 if( t == 0)
				p.Direction = ParameterDirection.Output;
			 p.ParameterName = "@" + Traiky.c_id;
			 p.Value = item.id;
			 Object obj = item.id; 
			int i = connec.ExecuteNonQuery("sp_Traiky", CommandType.StoredProcedure,p,ref obj,new SqlParameter("@flag", t),
				 new SqlParameter("@" + Traiky.c_tentraiky, item.tentraiky), new SqlParameter("@" + Traiky.c_ghichu, item.ghichu));
			if(t == 0)
				item.id = Convert.ToInt32(obj);
			return i;
		}

		public override Traiky GetItem(IDataReader dr)
		{
			Traiky obj = new Traiky();
			obj.id = Convert.ToInt32(dr[Traiky.c_id]);
			obj.tentraiky = Convert.ToString(dr[Traiky.c_tentraiky]);
			obj.ghichu = Convert.ToString(dr[Traiky.c_ghichu]);
			return obj;
		}

		public override List<Traiky> GetAll()
		{
			return GetAll("sp_GetAllTraiky", CommandType.StoredProcedure);
		}

		public override Traiky GetItemById(object id)
		{
			return GetAll().SingleOrDefault(p => p.id == Convert.ToInt32(id));
		}
		public override Traiky GetItemById(Traiky item){
			throw new NotImplementedException();
		}

	}

 }
